package com.bubblecam.view;


import java.util.ArrayList;

import com.bubblecam.activity.R;
import com.bubblecam.common.Element;
import com.bubblecam.thread.ViewThread;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;



public class DisplayPanel extends SurfaceView implements SurfaceHolder.Callback{

	private ViewThread viewThread;
	private int mX;
	private int mY;
	private ArrayList<Element> elements = new ArrayList<Element>();
	public static float mWidth;
	public static float mHeight;
	public static int fixedPosition = 0;
	public static int variablePosition = 0;

	public DisplayPanel(Context context) {
		super(context);
		getHolder().addCallback(this);
		viewThread = new ViewThread(this);
	}
	@Override
    public boolean onTouchEvent(MotionEvent event)
    {
    	mX = (int)event.getX();
    	mY = (int)event.getY();
    	synchronized (elements) {
    		Element element = new Element(getResources(),mX,mY);
    		elements.add(element);
    		variablePosition++;
			
		}
    	return super.onTouchEvent(event);
    }
	/* (non-Javadoc)
	 * @see android.view.View#onDraw(android.graphics.Canvas)
	 */
	public void doDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		canvas.drawColor(Color.BLACK);
		synchronized(elements)
    	{
    		for(Element element:elements)
    		{
    			element.doDraw(canvas);
    		}
    	}
		
	}
	public void animate(long elapsedTime)
	{
		synchronized(elements)
    	{
    		for(Element element:elements)
    		{
    			element.animate(elapsedTime);
    		}
    	}
	}
	

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		mWidth = width;
		mHeight  = height;
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		if(!viewThread.isAlive())
		{
			viewThread = new ViewThread(this);
			viewThread.setRunning(true);
			viewThread.start();
		}
		
		
	}	

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		if(viewThread.isAlive())
		{
			viewThread.setRunning(false);
		}
	}
	
	

}
